package com.basker.pisces.domain.property.meta;

import java.util.ArrayList;
import java.util.List;

import com.basker.pisces.domain.constant.DomainConstants.ObjectErrorCode;
import com.basker.pisces.domain.value.StringValue;
import com.basker.pisces.resource.StringResources;
import com.basker.pisces.service.exception.IObjectErrorsWrapper.ObjectError;
import com.basker.pisces.utils.IdCardUtils;

public class EmailProperty extends TextProperty {

    public EmailProperty() {

    }

    @Override
    protected String getDefaultInvalidInputValueMessage(String propertyDesc, Object inputValue) {
        return StringResources.get("pisces-domain.EmailProperty.InvalidEmail", propertyDesc, inputValue);
    }

    @Override
    protected List<ObjectError> validate(Object dataObject, StringValue stringValueObject) {
        List<ObjectError> errors = super.validate(dataObject, stringValueObject);

        String email = (String) stringValueObject.getDataValue();
        if (!stringValueObject.isEmpty() && !IdCardUtils.isValidatedAllIdcard(email)) {
            ObjectError objectError = new ObjectError(ObjectErrorCode.EMAIL_INVALID, dataObject, "",
                    this.getInvalidInputValueMessage(this.toString(), email));

            if (errors == null) {
                errors = new ArrayList<>();
            }

            errors.add(objectError);
        }

        return errors;
    }

}
